package com.jsh.erp.controller;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.ExceptionConstants;
import com.jsh.erp.datasource.entities.DailyHead;
import com.jsh.erp.datasource.entities.DailyHeadVo4Body;
import com.jsh.erp.datasource.entities.DepotHeadVo4Body;
import com.jsh.erp.service.dailyHead.DailyHeadService;
import com.jsh.erp.service.redis.RedisService;
import com.jsh.erp.utils.ErpInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;

/**
 * @author wy
 * @title: DaliyHeadController
 * @projectName JSH_ERP
 * @description:
 * @date 2023-01-12 19:37
 */
@RestController
@RequestMapping(value = "/dailyHead")
@Api(tags = {"每日对账主表"})
public class DailyHeadController {

    @Resource
    private RedisService redisService;
    @Resource
    private DailyHeadService dailyHeadService;

    @PostMapping(value = "/batchSetStatus")
    @ApiOperation(value = "批量设置状态-未核对或者已核对")
    public String batchSetStatus(@RequestBody JSONObject jsonObject,
                                 HttpServletRequest request) throws Exception {
        Map<String, Object> objectMap = new HashMap<>();
        String lanCode = redisService.getLanCodeFromSession(request);
        String status = jsonObject.getString("status");
        String ids = jsonObject.getString("ids");
        int res = dailyHeadService.batchSetStatus(status, ids);
        if (res > 0) {
            return returnJson(objectMap, "zh".equals(lanCode) ? ErpInfo.OK.name : ErpInfo.OK.lanCodeName, ErpInfo.OK.code);
        } else {
            return returnJson(objectMap, "zh".equals(lanCode) ? ErpInfo.ERROR.name : ErpInfo.ERROR.lanCodeName, ErpInfo.ERROR.code);
        }
    }

    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = DailyHead.class)
    })
    @PostMapping(value = "/addDailyHeadAndDetail")
    @ApiOperation(value = "新增单据主表及单据子表信息")
    public Object addDailyHeadAndDetail(@RequestBody DailyHeadVo4Body body, HttpServletRequest request) throws Exception {
        String lanCode = redisService.getLanCodeFromSession(request);
        JSONObject result = ExceptionConstants.standardSuccess(lanCode);
        String beanJson = body.getInfo();
        String rows = body.getRows();
        String attachment = body.getAttachment();
        Boolean passDirect = body.getPassDirect();
        String functionId = body.getFunctionId();

        dailyHeadService.addDailyHeadAndDetail(beanJson, rows, attachment, request, passDirect, functionId);
        return result;
    }

    /**
     * 更新单据主表及单据子表信息
     *
     * @param body
     * @param request
     * @return
     * @throws Exception
     */
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = DailyHead.class)
    })
    @PutMapping(value = "/updateDailyHeadAndDetail")
    @ApiOperation(value = "更新单据主表及单据子表信息")
    public Object updateDailyHeadAndDetail(@RequestBody DepotHeadVo4Body body, HttpServletRequest request) throws Exception {
        String lanCode = redisService.getLanCodeFromSession(request);
        JSONObject result = ExceptionConstants.standardSuccess(lanCode);
        String beanJson = body.getInfo();
        String rows = body.getRows();
        String attachment = body.getAttachment();
        Boolean passDirect = body.getPassDirect();
        String functionId = body.getFunctionId();
        dailyHeadService.updateDailyHeadAndDetail(beanJson, rows, attachment, request, passDirect, functionId);
        return result;
    }
}
